package com.wing.portal.aggregation;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.wing.cms.service.PlaceholderManager;
import com.wing.common.util.SpringDI;
/**
 * 
 * @author wyf
 * 
 */
public class PlaceholderFragment extends AbstractFragmentContainer {

	public PlaceholderFragment(Map param) {
		super(param);
		init(param);
	}

	@Override
	public void postService() {
	}

	@Override
	public void preService() {

	}

	public void init(Map<String,String> param) {
		PlaceholderManager placeholderManager = (PlaceholderManager) SpringDI
				.getBean("placeholderManager");
		List<Map<String, Object>> portletList = placeholderManager
				.findPortletIdListByPageAndHoder(param.get("pageId"), param.get("placeholder"));
		for (int i = 0; i < portletList.size(); i++) {
			Map<String,String> tempparams = new HashMap<String,String>();
			tempparams.putAll(param);
			tempparams.put("portletid",(String)(portletList.get(i).get("portletid")));
			tempparams.put("portletype",(String)(portletList.get(i).get("portletype")));
			Fragment pf = new PortletFragment(tempparams);
			this.addChild(pf);
		}

	}

	public String selfService() {
		StringBuffer sb = new StringBuffer();
		Iterator iterator = this.getChildFragments().iterator();
		while (iterator.hasNext()) {
			Fragment sub = (Fragment) iterator.next();
			sb.append(sub.service());
		}
		return sb.toString();
	}
}
